<?php

class Core_Frontend_Component_Ebook_Controller_Gallery extends Core_Frontend_Share_Component_Controller {

    public function showAction($id) {
        $model = App::getModel('media/ebook')->load($id);
        if (!$model->getId() || $model->getStatus() != 'published') {
            die('EBook可能已删除');
        }
        $content = $this->loadEbook($model->getSdUrl());
        $myhtml = '';
        if ($model->getAccessLimit()) {
            $myhtml .= '<div style="z-index: 6; position: absolute; right:0; top: 0px; width: 100px; height: 10px; margin-top: 6px;">
<a onclick="javascript:openHdShow(\'http://' . $this->getRequest()->getHostName() . $this->getUrl(array('id' => $id), 'hdshow').'\')" href="javascript:void(0)">高清版</a></div>';
        }
        $content = str_replace('</body>', $myhtml . '</body>', $content);
        echo $content;
        die();
    }

    public function hdshowAction($id) {
        $model = App::getModel('media/ebook')->load($id);
        if (!$model->getId() || $model->getStatus() != 'published') {
            die('EBook可能已删除');
        }
        if ($model->getAccessLimit()) {
            if ($model->getAccessPassword()) {
                return $this->viewResult(array('model' => $model));
            }
            $times = $model->getLimitTimes();
            if ($times > 0) {
                $model->updateLimitTimes();
            } else {
                die('EBook可能已删除');
            }
        }

        $content = $this->loadEbook($model->getHdUrl());
        echo $content;
        die();
    }

    public function hdshowPostAction($id) {
        $model = App::getModel('media/ebook')->load($id);
        if (!$model->getId() || $model->getStatus() != 'published') {
            die('EBook可能已删除');
        }

        if ($model->getAccessPassword() != $this->getRequest()->getParameter('password')) {
            return $this->viewResult(array('model' => $model));
        }

        $times = $model->getLimitTimes();
        if ($times > 0) {
            $model->updateLimitTimes();
        } else {
            die('EBook可能已删除');
        }

        $content = $this->loadEbook($model->getHdUrl());
        echo $content;
        die();
    }

    public function proxyAction() {
        $method = $this->getRequest()->getMethod();
        $url = base64_decode($this->getRequest()->getParameter('proxyurl'));
        $args = $this->getRequest()->getAllForm();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        curl_setopt($ch, CURLOPT_HEADER, 0);
        if (!empty($args)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
        }
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type'=>'application/x-www-form-urlencoded'));
        curl_exec($ch);
        $content = ob_get_clean();
        curl_close($ch);
        header('Content-Type:text/xml');
        echo $content;
        die();
    }

    private function loadEbook($url) {
        $urlInfo = parse_url($url);
        $ebookUrl = 'http://' . $urlInfo['host'];
        if (isset($urlInfo['port']) && $urlInfo['port'] != '80') {
            $ebookUrl .= ':' . $urlInfo['port'];
        }
        $ebookUrl .= str_replace('ebook.php', '', $urlInfo['path']);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        ob_start();
        curl_exec($ch);
        $content = ob_get_clean();
        curl_close($ch);
        $myhtml = '<script language="javascript" type="text/javascript" src="http://' . $this->getRequest()->getHostName() . '/pub/share/js/jquery/jquery.js"></script>';
        $myhtml .= '<script language="javascript" type="text/javascript" src="http://' . $this->getRequest()->getHostName() . '/pub/skin/stable/frontend/js/ebook.js"></script>';
        $content = str_replace('<head>', '<head><base href="' . $ebookUrl . '"/>', $content);
        $content = str_replace('../../../include/viewAudioObjectXml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewAudioObjectXml.php'), $content);
        $content = str_replace('../../../include/viewHyperLinkXml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewHyperLinkXml.php'), $content);
        $content = str_replace('../../../include/viewWordIndexXml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewWordIndexXml.php'), $content);
        $content = str_replace('../../../include/viewBookXmml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewBookXmml.php'), $content);
        $content = str_replace('../../../script/ajax.js', 'http://beta.fscsddt.com/pub/skin/stable/frontend/js/ebookajax.js', $content);
        $content = str_replace('</body>', $myhtml . '</body>', $content);
        return $content;
    }

}

